package com.qf.test;

import com.qf.util.ZKConnUtil;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.*;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

public class Demo1 {

    @Test
    public void ls() throws Exception {
        CuratorFramework cf = ZKConnUtil.cf();

        List<String> strings = cf.getChildren().forPath("/");

        for (String string : strings) {
            System.out.println(string);
        }
    }

    @Test  // create [-s] [-e]
    public void create()throws Exception{
        //1. 获取连接对象
        CuratorFramework cf = ZKConnUtil.cf();
        //2. 操作
        cf.create().withMode(CreateMode.PERSISTENT).forPath("/xxx","嘿嘿嘿".getBytes());
    }

    @Test  // delete  deleteall
    public void delete()throws Exception{
        //1. 获取连接对象
        CuratorFramework cf = ZKConnUtil.cf();
        //2. 操作
        cf.delete().deletingChildrenIfNeeded().forPath("/xxx0000000020");
        System.out.println("ok");
    }

    @Test
    public void set()throws Exception{
        //1. 获取连接对象
        CuratorFramework cf = ZKConnUtil.cf();
        //2. 操作
        cf.setData().forPath("/xxx","呵呵呵".getBytes());
        System.out.println("ok");
    }

    @Test
    public void get()throws Exception{
        //1. 获取连接对象
        CuratorFramework cf = ZKConnUtil.cf();
        //2. 操作
        byte[] bytes = cf.getData().forPath("/xxx");
        System.out.println(new String(bytes));
    }

    // czxid,mzxid
    @Test
    public void stat()throws Exception{
        //1. 获取连接对象
        CuratorFramework cf = ZKConnUtil.cf();
        //2. 操作
        Stat stat = cf.checkExists().forPath("/xxx");
        System.out.println(stat);
    }

    @Test   // 监听通知机制  ， 客户端监听Zookeeper上的某一个znode，当znode发生变化时，Zookeeper会主动通知监听这个znode的客户端
    public void listener() throws IOException {
        CuratorFramework cf = ZKConnUtil.cf();
        //1. 创建NodeCache(过时了..CuratorCache)的对象，指定监听的znode,实现类不能new，而且类还时被默认修饰符修饰的
        CuratorCache curatorCache = CuratorCache.builder(cf,"/xxx").build();

        //2. 设置监听的回调
        CuratorCacheListener listener = CuratorCacheListener.builder().forNodeCache(new NodeCacheListener() {
            @Override
            public void nodeChanged() throws Exception {
                ChildData childData = curatorCache.get("/xxx").get();
                Stat stat = childData.getStat();
                byte[] data = childData.getData();
                System.out.println("状态为：" + stat);
                System.out.println("数据为：" + new String(data));
            }
        }).build();
        curatorCache.listenable().addListener(listener);

        //3. 开启监听
        curatorCache.start();

        //4. System.in.read
        System.out.println("开始监听");
        System.in.read();
        /*
            Eureka为什么可以作为注册中心:  Eureka可以存数据,并且可以接收客户端请求,知道客户端请求的信息
            AP,

            Zookeeper为什么可以作为注册中心:Zookeeper可以存数据,并且有监听通知机制,变化立即通知客户端
            CP,
         */
    }

}
